var dropped = false;
var source;

var checked_in;
var checked_out;
var co;
var ci;



    $(document).ready(function() {
      
     
      
      //orders kids by their name
      function orderKids(divName){
        var names = Array();
        var childrenDivs = $("#" + divName).children();
        for (var i = 0; i < childrenDivs.length; i++){
          names.push(childrenDivs[i].id);
        }
        names.sort();
        
        var finger;
        for(var i = 0; i < names.length; i++){
          $("#" + names[0]).prependTo("#" + divName);
          finger = names[i];
          if(i + 1 < names.length){
            $("#" + names[i+1]).insertAfter("#" + finger);
          }
        }
      }
      
        //Gets checked in children
        $.getJSON('./utilities/children_model.php', {operation : 'get_checkedin'}, function(data){
              checked_in = data;
              for (var i = 0; i < checked_in.length; i++){
                ci = checked_in[i];
                var new_idcard;
                new_idcard = new IDcard(ci['name'], '.' + ci['picture'], ci['age'], ci['last_checked_in']);
                $("#CheckedIn").append(new_idcard.getDiv());
              }
              orderKids("CheckedIn");
              makeDraggable();
              //Double click functionality
              $("#CheckedIn .idcard").dblclick(function(){
                if(this.parentNode.id == 'CheckedOut'){
                  $('#CheckedIn').append(this);
                  check_in($(this).children('span.name').html());
                  orderKids("CheckedIn");
                  $('#toast').toast("You just checked in " + $(this).children('span.name').html());
                }
                else{  
                  $('#CheckedOut').append(this);
                  check_out($(this).children('span.name').html());
                  orderKids("CheckedOut");
                  $('#toast').toast("You just checked out " + $(this).children('span.name').html());
                }
              });
        });   

        //Gets checked out children
        $.getJSON('./utilities/children_model.php', {operation : 'get_checkedout'}, function(data){
              checked_out = data;
              for (var i = 0; i < checked_out.length; i++){
                co = checked_out[i];
                var new_idcard;
                new_idcard = new IDcard(co['name'], '.' + co['picture'], co['age'], co['last_checked_in']);
                $("#CheckedOut").append(new_idcard.getDiv());
              }   
              orderKids("CheckedOut");
              makeDraggable(); 
              //Double click functionality
              $("#CheckedOut .idcard").dblclick(function(){
                  if(this.parentNode.id == 'CheckedOut'){
                    $('#CheckedIn').append(this);
                    check_in($(this).children('span.name').html());
                    orderKids("CheckedIn");
                    $('#toast').toast("You just checked in " + $(this).children('span.name').html());
                  }
                  else{ 
                    $('#CheckedOut').append(this);
                    check_out($(this).children('span.name').html());
                    orderKids("CheckedOut");
                    $('#toast').toast("You just checked out " + $(this).children('span.name').html());
                  }
                });
        });

      
    
    //This is kind of unnecessary, and I should refactor this... 
    function update_arrays(){
      $.getJSON('./utilities/children_model.php', {operation : 'get_checkedin'}, function(data){
            checked_in = data;
      });
      
      $.getJSON('./utilities/children_model.php', {operation : 'get_checkedout'}, function(data){
            checked_out = data;
      });
    }
    
    function check_in(name){
      $.get('./utilities/children_model.php', {operation : 'check_in', name : name}, function(data){
        update_arrays();
      });
      $.post('./utilities/log_model.php', {operation : 'insert_entry', type : 'check_in', 'names[]' : [name]});
    }
    
    function check_out(name){
      $.get('./utilities/children_model.php', {operation : 'check_out', name : name}, function(data){
        update_arrays();
      });
      $.post('./utilities/log_model.php', {operation : 'insert_entry', type : 'check_out', 'names[]' : [name]});
      
    }
    
    // var Chuck = new IDcard("Chuck Finster", "./images/Chuck.jpg", 5, 23);  
    // var Angelica = new IDcard("Angelica Pickles", "./images/Angelica.jpg", 6, 23);
    // var Jedidiah = new IDcard("Jedidiah Bitslow", "./images/Jedidiah.jpeg", 3, 24);
    // var Kimi = new IDcard("Kimi Finster", "./images/Kimi.jpg", 5, 25);
    // var Lilly = new IDcard("Lilly Deville", "./images/Lil.jpg", 5, 25);
    // var Tommy = new IDcard("Tommy Pickles", "./images/Tommy.jpg", 5, 23);
    // var Susie = new IDcard("Susie Carmichael", "./images/Susie.jpg", 6, 24);
     
    // $("#CheckedOut").append(Chuck.getDiv());
    // $("#CheckedOut").append(Angelica.getDiv());
    // $("#CheckedOut").append(Jedidiah.getDiv());
    // $("#CheckedOut").append(Kimi.getDiv());
    // $("#CheckedOut").append(Lilly.getDiv());
    // $("#CheckedOut").append(Tommy.getDiv());
    // $("#CheckedOut").append(Susie.getDiv());

		function makeDraggable() {
			$('.idcard').draggable({
				containment : 'window',
				stack : '#CheckedOut',
				cursor : 'move',
				revert : 'invalid',
				appendTo : 'body',
				scroll : false,
				helper : 'clone',
				start : function(event, ui) {
					dropped = false;
					$(this).addClass("hide");
					source = this.parentNode.id;
					$(ui.helper).addClass("dragged");
				},
				stop : function(event, ui) {
				  $(this).removeClass("hide");
				}
			});
		}
    
		$('#CheckedIn_wrapper').droppable({
			accept : '.idcard',
			drop : function(event, ui) {
			  if(source == 'CheckedIn'){//do nothing
			  }
			  else{
			    dropped = true; 
          // hacky
			    ui.draggable.children('span.lastCheckin').html("Last Checked in: <span>Just now</span>");         
  				$('#CheckedIn').append(ui.draggable);
  				check_in(ui.helper.children('span.name').html());
  				orderKids("CheckedIn");
  				$('#toast').toast("You just checked in " + ui.helper.children('span.name').html());
  				makeDraggable();
			  }
			}
		});

		$('#CheckedOut_wrapper').droppable({
			accept : '.idcard',
			drop : function(event, ui) {
			  if(source == 'CheckedOut'){//do nothing
			  }
			  else{
			    dropped = true;
  				$('#CheckedOut').append(ui.draggable);
  				check_out(ui.helper.children('span.name').html());
  				orderKids("CheckedOut");
  				$('#toast').toast("You just checked out " + ui.helper.children('span.name').html());
  				makeDraggable();
			  }
			}
		});
		

    $("#btnOk").click(function() {
     $("#btnOk").attr("disabled", "true");
     setTimeout(function() {
       window.location.href = "./"
     }, 1);
     // $('#toast').toast("Checked In/Out information submitted");
    });
		
	});
